package cn.edu.scau.cmi.wangmaoren.client.hibenate;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.scau.cmi.wangmaoren.hibernate.domain.Major;

public class HibernateClient2JdbcEntity
{
	public static void main(String[] args)
	{
		Major major = new Major();
		major.setName("软件工程");
		major.setId(2L);

		newMajor(major);
		listAllmajors();
	}

	private static int newMajor(Major major)
	{
		Connection conn = getConn();
		int i = 0;
		String sql = "insert into major (id,name) values(?,?)";
		PreparedStatement pstmt;
		try
		{
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			pstmt.setLong(1, major.getId());
			pstmt.setString(2, major.getName());
			i = pstmt.executeUpdate();
			pstmt.close();
			conn.close();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return i;
	}

	private static int update(Major major)
	{
		Connection conn = getConn();
		int i = 0;
		String sql = "update major set id='" + major.getId() + "' where Name='" + major.getName() + "'";
		PreparedStatement pstmt;
		try
		{
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			i = pstmt.executeUpdate();
			System.out.println("resutl: " + i);
			pstmt.close();
			conn.close();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return i;
	}

	private static Integer listAllmajors()
	{
		Connection conn = getConn();
		String sql = "select * from major";
		PreparedStatement pstmt;
		try
		{
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			int col = rs.getMetaData().getColumnCount();
			System.out.println("============================");
			while (rs.next())
			{
				for (int i = 1; i <= col; i++)
				{
					System.out.print(rs.getString(i) + "\t");
					if ((i == 2) && (rs.getString(i).length() < 8))
					{
						System.out.print("\t");
					}
				}
				System.out.println("");
			}
			System.out.println("============================");
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private static int delete(String name)
	{
		Connection conn = getConn();
		int i = 0;
		String sql = "delete from major where Name='" + name + "'";
		PreparedStatement pstmt;
		try
		{
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			i = pstmt.executeUpdate();
			System.out.println("resutl: " + i);
			pstmt.close();
			conn.close();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return i;
	}

	private static Connection getConn()
	{
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/sa";
		String username = "root";
		String password = "";
		Connection conn = null;
		try
		{
			Class.forName(driver); // classLoader,加载对应驱动
			conn = (Connection) DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return conn;
	}

}